package com.general.app.answer.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.general.app.answer.dao.entity.Answer;
import com.general.app.answer.service.IAnswerService;
import com.general.app.wjgl.dao.entity.Question;
import com.general.app.wjgl.dao.entity.Survey;
import com.general.app.wjgl.dao.entity.SurveySendModel;
import com.general.app.wjgl.service.IQuestionService;
import com.general.app.wjgl.service.ISurveySendService;
import com.general.app.wjgl.service.ISurveyService;
import com.general.app.xtgl.dao.entity.AssessUserModel;
import com.general.app.xtgl.service.IAssessUserService;
import com.general.common.action.BaseAction;
import com.general.common.model.QueryModel;
import com.general.common.security.entity.User;
import com.general.utils.base.Constant;
import com.general.utils.base.StringUtil;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;
/**
 * 问卷回答[个人]
 * @author Administrator
 *
 */
public class AnswerAction extends BaseAction implements ModelDriven<Answer> {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * 保存
	 */
	private static final String TYPE_SAVE = "save";
	/**
	 * 提交
	 */
	private static final String TYPE_SUBMIT = "submit";	
	private final static String SPLIT_CHAR = "_";

	private Answer model = new Answer();

	private IAssessUserService assessUserService;
	private IAnswerService answerService;
	private IQuestionService questionService;
	private ISurveyService surveyService;
	private ISurveySendService surveySendService;
	
	/**
	 * 个人回答列表
	 * @return
	 */
	public String answerList(){
		HttpSession session = getSession();
		
		String usertype  = (String)session.getAttribute("userType");
		// 如果是答卷者
		if(Constant.USER_TYPE_ANSWER.equals(usertype)){
			User user = getCurrentUser();
			model.setUser_id(user.getUserid());
		}
		ValueStack vs = getValueStack();		
		vs.set("usertype", usertype);
		
		if (QUERY.equals(model.getDoType())) {
			QueryModel queryModel = model.getQueryModel();
			try {
				queryModel.setItems(answerService.getPagedList(model));
			} catch (Exception e) {
				e.printStackTrace();
			}
			getValueStack().set(DATA, queryModel);
			return DATA;
		}

		// 如果是答卷者
		if(Constant.USER_TYPE_ANSWER.equals(usertype)){
			return "answerList";
		}else{
			return "answerManagerList";
		}
	}	
	
	/**
	 * 个人回答列表
	 * @return
	 */
	public String answerListPhone(){
		HttpSession session = getSession();
		
		String usertype  = (String)session.getAttribute("userType");
		// 如果是答卷者
		if(Constant.USER_TYPE_ANSWER.equals(usertype)){
			User user = getCurrentUser();
			model.setUser_id(user.getUserid());
		}
		ValueStack vs = getValueStack();		
		vs.set("usertype", usertype);

		try {
			List<Answer> answerlst = answerService.getModelListByScope(model);
			vs.set("answerlst", answerlst);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "answerListPhone";
	}	
	
	/**
	 * 访问答卷页面
	 * @return
	 */
	public String showSurveyAnswer(){

		String answerId = model.getSend_survey_user_assuser();
		
		Map<String,String> map = new HashMap<String, String>();
		String[] infoArr = answerId.split(SPLIT_CHAR);
		String sendId = infoArr[0];
		String surveyId = infoArr[1];
		String userId = infoArr[2];
		String assUserId = infoArr[3];
		map.put("send_id", sendId);
		map.put("user_id", userId);
		map.put("assess_user_id", assUserId);
		map.put("survey_id", surveyId);
		
		SurveySendModel surveySend = new SurveySendModel();
		surveySend.setSend_id(sendId);
		surveySend.setSurvey_id(surveyId);
		surveySend.setUser_id(userId);
		surveySend.setAssess_user_id(assUserId);
		SurveySendModel send = surveySendService.getModel(surveySend);
		
		//问卷已结束/已提交
		if(send != null && (Constant.SURVEY_FLG_END.equals(send.getSurvey_flg())
				|| Constant.SUBMIT_FLG_YES.equals(send.getSubmit_flg()))){

			model.setUser_id(getCurrentUser().getUserid());
			ValueStack vs = getValueStack();	
			List<Answer> answerlst = answerService.getModelListByScope(model);
			vs.set("answerlst", answerlst);
			vs.set(DATA, "该问卷已结束或者您已提交！");
			return "answerListPhone";
		}
		
		String relation = model.getRelation();
		if (relation != null && !StringUtil.isEmpty(relation)) {
			if(Constant.SUPERIOR.equals(relation)){
				model.setRelation_name("上级");
			}else if(Constant.EQUAL.equals(relation)){
				model.setRelation_name("平级");
			}else if(Constant.SUBORDINATE.equals(relation)){
				model.setRelation_name("下属");
			}else if(Constant.SELF.equals(relation)){
				model.setRelation_name("自评");
			}
		}
		AssessUserModel assess = assessUserService.getModel(assUserId);
		if(assess != null){
			model.setAssess_user_nm(assess.getUser_nm());
		}
		// 问卷信息
		Survey survey = surveyService.getModel(surveyId);
		ValueStack vs = getValueStack();
		vs.set("survey", survey);

		//试题列表信息
		List<Question> questionList = questionService.getSurveyQuestionAnswerList(map);
		vs.set("questionList", questionList);
		

		HttpServletRequest request = getRequest();
		String type = request.getParameter("type");
		if("phone".equals(type)){
			return "showSurveyAnswerPhone";
		}
		return "showSurveyAnswer";
	}
	
	
	/**
	 * 答卷（文本题）
	 * @return
	 */
	public String saveTextQuestion(){
		
		HttpServletRequest request = getRequest();
		String id = request.getParameter("id");
		String qId = request.getParameter("qId");
		String answer = request.getParameter("answer");
		String relation = request.getParameter("relation");
		
		boolean result = answerService.saveTextAnswer(id, relation, qId, answer);
		getValueStack().set(DATA, result);
		
		return DATA;
	}
	
	
	/**
	 * 答卷（单选题）
	 * @return
	 */
	public String saveSingleQuestion(){
		
		HttpServletRequest request = getRequest();
		String id = request.getParameter("id");
		String qId = request.getParameter("qId");
		String oId = request.getParameter("oId");
		String relation = request.getParameter("relation");
		String score = request.getParameter("score");
		
		boolean result = answerService.saveSingleQuestion(id, relation, qId, oId, score);
		getValueStack().set(DATA, result);
		
		return DATA;
	}
	
	/**
	 * 答卷（复选题取消选择）
	 * @return
	 */
	public String delMultipleAnswer(){
		
		HttpServletRequest request = getRequest();
		String id = request.getParameter("id");
		String qId = request.getParameter("qId");
		String oId = request.getParameter("oId");
		String relation = request.getParameter("relation");
		
		boolean result = answerService.delMultipleAnswer(id, relation, qId, oId);
		getValueStack().set(DATA, result);
		return DATA;
	}
	
	/**
	 * 答卷（复选题）
	 * @return
	 */
	public String saveMultipleQuestion(){
		HttpServletRequest request = getRequest();
		String id = request.getParameter("id");
		String qId = request.getParameter("qId");
		String oId = request.getParameter("oId");
		String relation = request.getParameter("relation");
		String score = request.getParameter("score");
		
		boolean result = answerService.saveMultipleAnswer(id, relation, qId, oId, score);
		getValueStack().set(DATA, result);
		return DATA;
	}
	
	
	
	/**
	 * 提交试题答案
	 * @return
	 */
	public String submitAnswer(){
		HttpServletRequest request = getRequest();
		String id = request.getParameter("id");
		String relation = request.getParameter("relation");
		String type = request.getParameter("type");	

		HashMap<String, Object> map=new HashMap<String, Object>();
		SurveySendModel send = new SurveySendModel();
		List<SurveySendModel> list = new ArrayList<SurveySendModel>();
		String message = "";
		if(TYPE_SAVE.equals(type)){
			map.put("submit_flg", Constant.SUBMIT_FLG_SAVE);
			message = "保存";
			
		}else if(TYPE_SUBMIT.equals(type)){
			map.put("submit_flg", Constant.SUBMIT_FLG_YES);		
			message = "提交";	
		}

		String[] infoArr = id.split(SPLIT_CHAR);
		send.setSend_id(infoArr[0]);
		send.setSurvey_id(infoArr[1]);
		send.setUser_id(infoArr[2]);
		send.setAssess_user_id(infoArr[3]);
		send.setRelation(relation);
		

		SurveySendModel surveySend = new SurveySendModel();
		surveySend.setSend_id(infoArr[0]);
		surveySend.setSurvey_id(infoArr[1]);
		surveySend.setUser_id(infoArr[2]);
		surveySend.setAssess_user_id(infoArr[3]);
		SurveySendModel sendNew = surveySendService.getModel(surveySend);
		
		//问卷已结束/已提交,返回失败
		if(send != null && (Constant.SURVEY_FLG_END.equals(sendNew.getSurvey_flg())
				|| Constant.SUBMIT_FLG_YES.equals(sendNew.getSubmit_flg()))){

			getValueStack().set(DATA, message+"失败！");
			return DATA;
		}
		
		list.add(send);
		map.put("list", list);
		int count = surveySendService.batchUpdSubmitFlg(map);
		message = count>0 ? message+"成功！" : message+"失败！";
		getValueStack().set(DATA, message);
		return DATA;
	}

	
	public ISurveySendService getSurveySendService() {
		return surveySendService;
	}

	public void setSurveySendService(ISurveySendService surveySendService) {
		this.surveySendService = surveySendService;
	}

	public IQuestionService getQuestionService() {
		return questionService;
	}

	public ISurveyService getSurveyService() {
		return surveyService;
	}

	public Answer getModel() {
		return model;
	}

	public void setModel(Answer model) {
		this.model = model;
	}

	public IAnswerService getAnswerService() {
		return answerService;
	}

	public void setAnswerService(IAnswerService answerService) {
		this.answerService = answerService;
	}
	
	
	public void setQuestionService(IQuestionService questionService) {
		this.questionService = questionService;
	}

	public void setSurveyService(ISurveyService surveyService) {
		this.surveyService = surveyService;
	}

	public IAssessUserService getAssessUserService() {
		return assessUserService;
	}

	public void setAssessUserService(IAssessUserService assessUserService) {
		this.assessUserService = assessUserService;
	}
	
}
